const https = require('https')
const fs = require('fs')
const bodyParser = require('body-parser')
const express = require('express')
const configs = require('./config')
const routers = require('./routers/index.js')
const app = express()
// 共52个接口


/**
 * 静态资源
 */
app.use(express.static('./dist'));
app.use(express.static('./public'));
app.get('/',function (req,res) {
    res.header("Access-Control-Allow-Credentials", "true")
	  res.header("Access-Control-Allow-Origin", "*")
    res.sendFile("./dist/index.html")
})



/**
 * 第三方参数中间件
 */
app.use(bodyParser.urlencoded({extend:false}))
app.use(bodyParser.json())



/**
 * 自己开发的中间件
 */
const cross_domain_middlewere = require('./middlewere/cross_domain') // 处理跨域
const admin_middlewere = require('./middlewere/admin')  // 管理员身份验证
const student_middlewere = require('./middlewere/studentPermission') // 学生身份验证
const check_parameter = require('./middlewere/check_parameter')  // 参数检验
const xingzheng_middlewere = require('./middlewere/xingzheng_teacher') // 行政老师身份验证
const jiaoyan_middlewere = require('./middlewere/jiaoyan_middlewere') // 教研老师身份验证
const general_middlewere = require('./middlewere/general_middlewere') // 通用接口



/**
 * login and logout router
 */
app.use(
    '/', 
    cross_domain_middlewere,
    check_parameter,
    routers.LoginRouter
)



/**
 * admin router
 */
const AdminRouters = require('./routers/admin')
app.use(
  '/admin', 
  cross_domain_middlewere, 
  admin_middlewere,
  check_parameter,
  AdminRouters
)



/**
 * xing zheng teacher
 */
const XingZhengRouter = require('./routers/xingzheng')
app.use(
  '/xingzheng',
  cross_domain_middlewere,
  xingzheng_middlewere,
  check_parameter,
  XingZhengRouter
)

   

/**
 * jiao yan teacher
 */
const JiaoYanRouter = require('./routers/jiaoyan')
app.use(
  '/jiaoyan',
  cross_domain_middlewere,
  jiaoyan_middlewere,
  check_parameter,
  JiaoYanRouter
)


/**
 * student router
 */
const StudentRouters = require('./routers/student')
app.use(
  '/student',
  cross_domain_middlewere,
  student_middlewere,
  check_parameter,
  StudentRouters
)



/**
 * general router
 */
const GeneralAPIRouter = require('./routers/generalAPI')
app.use(
  '/general',
  cross_domain_middlewere,
  general_middlewere,
  check_parameter,
  GeneralAPIRouter
)



/**
 * handle 404
 */
app.use("*", (req, res, next) => {
  res.json({
    code: 404,
    message: '兄弟你路径是不是搞错了！'
  })
})



/**
 * host and port config
 */

 https.createServer({
   key: fs.readFileSync('./3081777_kujijiku.com.key'),
   cert: fs.readFileSync('./3081777_kujijiku.com.crt')
 },app).listen(configs.server_config.PORT, configs.server_config.HOST, () => {
  console.log("server started at: ", configs.server_config.HOST + ":" + configs.server_config.PORT)
 })


// app.listen(configs.server_config.PORT, configs.server_config.HOST, function() {
// 	console.log("server started at: ", configs.server_config.HOST + ":" + configs.server_config.PORT)
// })
// process.on('uncaughtException', (err) => {
//   console.log(err.stack);
// });


